import { Repository, Like } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { User } from '../../entity/user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User) private readonly userRepository: Repository<User>,
  ) {}

  async createUser(user: User): Promise<User> {
    return await this.userRepository.save(user);
  }

  async checkNameExist(name: string): Promise<boolean> {
    const exist = await this.userRepository.exists({
      where: { username: name },
    });
    return exist;
  }

  async getUserByName(name: string): Promise<User> {
    return await this.userRepository.findOne({
      where: { username: name },
    });
  }

  async getUserById(id: number): Promise<User> {
    return await this.userRepository.findOne({
      where: { id: id },
    });
  }
  async findAll(): Promise<User[]> {
    return this.userRepository.find();
  }
  async delete(id: number): Promise<void> {
    await this.userRepository.delete(id);
  }
}
